#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;
enum { max_length = 1024 };

int main(int argc,char *argv[])
{
    try
    {
        if(argc != 3)
        {
            std::cerr<<"Usage main <host> <port>\n";
            return 1;
        }

        boost::asio::io_service io_service;

        tcp::socket socket(io_service);

        //解析器
        tcp::resolver resolver(io_service);
        boost::asio::connect(socket,resolver.resolve({argv[1],argv[2]}));

        std::cout<<"Enter message: ";

        //请求数据
        char request[max_length];
        //应答数据
        char reply[max_length];
        while(1)
        {
             //输入一行到request这个数组，一共输入max_length长度
             std::cin.getline(request,max_length);
             size_t request_length = std::strlen(request);

             //写给服务器
             boost::asio::write(socket,boost::asio::buffer(request,request_length));

             size_t reply_length = boost::asio::read(socket,boost::asio::buffer(reply,request_length));

            std::cout<<"Reply is: ";
            std::cout.write(reply,reply_length);
            std::cout<<std::endl;
        }
    } 
    catch(std::exception &ec)
    {
        std::cerr<<"Exception:"<<ec.what()<<"\n";
    }

    return 0;
}

